package day2test3;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Scanner;

//1,一个停车场,内有多个车位,可停入各种车辆
//2·只有具备车牌并高度低于
//3米的车辆可停入3,停入时开始计费,按每小时2元
//4,查询全部停车位的状态
//5,按车牌及停车位号取车,取车时收停车费,查询统计全部的收费记录
public class Function {

	static Parkinglot parkinglot = new Parkinglot();
	static Scanner scanner = new Scanner(System.in);
	static ArrayList<Car> cars = new ArrayList<Car>();

	// 初始化

	public static void initialize() throws Exception {

		String[] idstate = new String[200];
		for (int i = 0; i < 200; i++) {
			idstate[i] = "空闲";
		} 
		parkinglot.setIdstate(idstate);
		ArrayList<String> record = new ArrayList<String>();
		parkinglot.setRecord(record);

		FileOutputStream fos1 = new FileOutputStream("parkinglot.txt");
		ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
		oos1.writeObject(parkinglot);
		oos1.flush();
		oos1.close();
		FileOutputStream fos2 = new FileOutputStream("cars.txt");
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
		oos2.writeObject(cars);
		oos2.flush();
		oos2.close();
		
		System.out.println("信息初始化成功");
	}

	// 读文件 parkinglot
	public static Parkinglot ReadParkinglotTxt() throws Exception {
		FileInputStream fis1 = new FileInputStream("parkinglot.txt");

		ObjectInputStream oin1 = new ObjectInputStream(fis1);

		parkinglot = (Parkinglot) oin1.readObject();

		oin1.close();
		fis1.close();
		return parkinglot;
	}

	// 写文件 parkinglot
	public static void WirteParkinglotTxt(Parkinglot parkinglot) throws Exception {
		FileOutputStream fos1 = new FileOutputStream("parkinglot.txt");
		ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
		oos1.writeObject(parkinglot);
		oos1.flush();
		oos1.close();
	}

	// 读文件 cards
	public static ArrayList<Car> ReadCarsTxt() throws Exception {
		FileInputStream fis2 = new FileInputStream("cars.txt");

		ObjectInputStream oin2 = new ObjectInputStream(fis2);

		cars = (ArrayList<Car>) oin2.readObject();

		oin2.close();
		fis2.close();
		return cars;
	}

	// 写文件 cards
	public static void WirteCarsTxt(ArrayList<Car> cars) throws Exception {
		FileOutputStream fos2 = new FileOutputStream("cars.txt");
		ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
		oos2.writeObject(cars);
		oos2.flush();
		oos2.close();
	}

	// 增加车辆信息

	public void addCar() throws Exception {
		cars = ReadCarsTxt();
		Car car = new Car();
		System.out.println("请输入车牌");
		String id = scanner.nextLine();
		car.setId(id);
		System.out.println("请输入车高(精确到小数点后两位)");
		double high = Double.parseDouble(scanner.nextLine());
		car.setHigh(high);
		cars.add(car);
		WirteCarsTxt(cars);

	}

	// 汽车停车
	// 条件1： 停车场有车位 2：车的高度小于3
	// 操作： 1.停车场车位减一 2. 设置停车时时间 3//设置停车位号状态
	public void CarPaking() throws Exception {
		cars = ReadCarsTxt();
		parkinglot = ReadParkinglotTxt();
		System.out.println("请输入车牌号");
		String id = scanner.nextLine();
		for (Car car : cars) {
			if (id.equals(car.getId())) {

				double high = car.getHigh();
				if (high > 3) {
					System.out.println("对不起您的车辆太高，禁止停车");
				} else {

					// 获取停车场车位,有车位才能停
					int space = parkinglot.getSpace();
					if (space <= 0) {
						System.out.println("对不起停车场车位已经满了");
					} else {
					

						// 设置停车位号
						System.out.println("请输入停车位号");
						int space_id = Integer.parseInt(scanner.nextLine());
						// 把停车场号 给 car 取车操作需用 
						String[] space_state = parkinglot.getIdstate();
						if ("空闲".equals(space_state[space_id - 1])) {

							car.setSpace_id(space_id);
							
							// 设置停车时间，
							System.out.println("请输入停车时间(yyyy-MM-dd HH:mm:ss SSS)");
							String hour = scanner.nextLine();

							// 设置停车时间
							car.setTime1(hour);

							// 设置停车场车位减一
							parkinglot.setSpace(space - 1);

							String[] idstate = parkinglot.getIdstate();
							idstate[space_id - 1] = "占用";
							parkinglot.setIdstate(idstate);

						} else {
							System.out.println("对不起该车位已经有车辆了");
						}

					}
				}
			}
		}
		
		WirteCarsTxt(cars);
		WirteParkinglotTxt(parkinglot);
	}

	// 查询全部停车位的状态
	public void findAllState() throws Exception {
		
		parkinglot = ReadParkinglotTxt();
		
		int space = parkinglot.getSpace();
		System.out.println("停车场空闲车位共有：  " + space);
		String[] idstate = parkinglot.getIdstate();
		if (idstate == null) {
			System.out.println("没有记录");
		}
		for (int i = 0; i < 200; i++) {
			System.out.println("停车位" + (i + 1) + "的停车状态为:   " + idstate[i]);
		}
	}

	// 取车操作 按车牌及停车位号取车,取车时收停车费
	public void GetCar() throws Exception {

		cars = ReadCarsTxt();
		parkinglot = ReadParkinglotTxt();
	
		System.out.println("请输入车牌号");
		String id = scanner.nextLine();
		System.out.println("请输入停车场号");
		int space_id = Integer.parseInt(scanner.nextLine());
		boolean falg = false;
		for (Car car : cars) {
			// 判断车牌号和停车位是否准确
			if (id.equals(car.getId()) && car.getSpace_id() == space_id) {
				falg = true;
				System.out.println("请输入取车时间(yyyy-MM-dd HH:mm:ss SSS)");
				String hour = scanner.nextLine();

				// 设置取车时间
				car.setTime2(hour);

				// 计算车费
				String hour1 = car.getTime1();
				Calendar calendar1 = Calendar.getInstance();

				try {
					calendar1.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(hour));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				System.out.println(calendar1.getTimeInMillis());

				long H1 = calendar1.getTimeInMillis();
//				System.out.println(H1);
				Calendar calendar2 = Calendar.getInstance();
				try {
					// 将时间字符串格式转化为毫秒
					calendar2.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(hour1));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				long H2 = calendar2.getTimeInMillis();

				// 把时间转化为小时
				double time1 = (H1 - H2) / 1000 / 60 / 60.0;

				// 利用bigdecimal 保留两位数（向上取整）
				BigDecimal bigDecimal = new BigDecimal(time1);
				double time = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

				System.out.println("停车时长为： " + time);
				double cost = time * 2;
				System.out.println("本次消费为： " + cost + " 元");
				car.setCost(cost);

				// 停车场车位要加1
				int space = parkinglot.getSpace();
				parkinglot.setSpace(space + 1);

				// 停车场号的 状态变为空闲

				String[] idstate = parkinglot.getIdstate();
				idstate[space_id] = "空闲";

				// 停车场记录
				ArrayList<String> record = parkinglot.getRecord();

				record.add("车牌号为 " + id + "的车辆,在停车场号为： " + space_id + "共停车" + time + "小时,花费" + cost + "元 ");
				parkinglot.setRecord(record);
				// 全部收费 增加
				double toll = parkinglot.getToll();
				parkinglot.setToll(toll + cost);

				break;

			}
		}
	
		WirteCarsTxt(cars);
		WirteParkinglotTxt(parkinglot);
		if (falg == false) {
			System.out.println("对不起，不找到您所描述的车辆");
		}
	}

	// 查询全部的收费记录
	public void finAllCost() throws Exception {
		
		parkinglot = ReadParkinglotTxt();
		
		ArrayList<String> records = parkinglot.getRecord();
		if (records.size() == 0) {
			System.out.println("没有记录");
		}
		int i = 1;
		for (String record : records) {

			System.out.println(i + ":" + record);
			i++;
		}
	}

	// 菜单展示
	public void showMenu() {
		System.out.println("********  首次登录必须初始化信息 ********");
		System.out.println("******** 1.初始化信息      ********");
		System.out.println("******** 2.添加车辆        ********");
		System.out.println("******** 3.停车           ********");
		System.out.println("******** 4.取车           ********");
		System.out.println("******** 5.查询所有停车场状态 ********");
		System.out.println("******** 6.查询所有收费信息   ********");
		System.out.println("******** 7.查询指定车辆停车信息   ********");
		System.out.println("******** 8.查询所有车辆信息   ********");
	}

	// 菜单选择栏
	public void chooseMenu() throws Exception {
		while (true) {
			showMenu();
			System.out.println("请输入您要选择的功能");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1": {
				initialize();
				break;
			}
			case "2": {
				addCar();
				break;
			}
			case "3": {
				CarPaking();
				break;
			}
			case "4": {
				GetCar();
				break;
			}
			case "5": {
				findAllState();
				break;
			}
			case "6": {
				finAllCost();
				break;
			}
			case "7": {
				findCar();
				break;
			}
			case "8": {
				finAllParkingCar();
				break;
			}
			default:
				throw new IllegalArgumentException("输入错误 ");
			}

			System.out.println("退出请输入 # ,继续请输入任意键");
			String flag = scanner.nextLine();
			if ("#".equals(flag)) {
				break;
			}
		}
	}

	// 查询某个车辆的信息
	public void findCar() throws Exception {
		cars = ReadCarsTxt();
		
		
		System.out.println("请输入车牌号");
		String id = scanner.nextLine();
		boolean flag = false;
		int i = 1;
		for (Car car : cars) {

			if (id.equals(car.getId())) {
				if (car.getHigh() > 3) {
					System.out.println("对不起,没有此车的停车信息");
				} else {
					double high = car.getHigh();
					String time1 = car.getTime1();
					String time2 = car.getTime2();
					double cost = car.getCost();
					int space_id = car.getSpace_id();

					System.out.println(i + ": 车牌号:" + id + ",停车时间:" + time1 + ",取车时间:" + time2 + ",所停车位：" + space_id
							+ ",总共花费" + cost + "元。");
					i++;
					flag = true;
					break;
				}
			}
		}
		if (flag == false) {
			System.out.println("查不到所描述的车辆");
		}
	}

	// 查询所有车辆信息
	public void finAllParkingCar() throws Exception {
		cars = ReadCarsTxt();
	
		boolean flag = false;
		int i = 1;
		for (Car car : cars) {
			String id = car.getId();
			double high = car.getHigh();
			String time1 = car.getTime1();
			String time2 = car.getTime2();
			double cost = car.getCost();
			int space_id = car.getSpace_id();

			System.out.println(i + ": 车牌号:" + id + ",停车时间:" + time1 + ",取车时间:" + time2 + ",所停车位：" + space_id + ",总共花费"
					+ cost + "元。");
			i++;
			flag = true;

		}

		if (flag == false) {
			System.out.println("没有记录");
		}
	}

}
